package com.fhz.mvitest2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.fhz.mvitest.viewmodel.BannerViewModel
import com.fhz.mvitest2.databinding.ActivityMainBinding
import com.fhz.mvitest2.intent.BannerIntent
import com.fhz.mvitest2.intent.LoginIntent
import com.fhz.mvitest2.state.LoginUIState
import com.fhz.mvitest2.ui.BannerUIState
import com.fhz.mvitest2.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private val veiwmodel: BannerViewModel by viewModels()
    private val loginViewModel: LoginViewModel by viewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)



        binding.login.setOnClickListener{
            lifecycleScope.launch {
                loginViewModel.channel.send(LoginIntent.Login("q1","1"))
            }
        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                loginViewModel.state.collect{
                    when(it){
                        is LoginUIState.Success -> {
                            println(it.res.errorMsg)
                        }

                        is LoginUIState.Fail -> {
                            println(it.res.errorMsg)
                        }
                        is LoginUIState.loading -> {

                        }
                        else -> {}
                    }

                }
            }
        }

//        lifecycleScope.launch{
//            veiwmodel.channel.send(BannerIntent.GetBannerList)
//        }
//
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED){
//                veiwmodel.state.collect{
//                    when(it){
//                        is BannerUIState.Success -> {
//                            println("成功")
//                            println(it.banner.data.size)
//                        }
//                        is BannerUIState.Fail -> {
//                            println("失败")
//                        }
//                        else -> {
//                            println("其他")
//                        }
//                    }
//                }
//            }
//
//        }

    }
}